﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Shoozla.ViewModel;
using Shoozla.DataModel;
using System.Threading;
using Shoozla.DataModel.Helper;

namespace Shoozla.View
{
    /// <summary>
    /// Interaction logic for ProgressBarView.xaml
    /// </summary>
    public partial class SearchingNagScreenView : Window
    {        
        public SearchingNagScreenView():base()
        {
            InitializeComponent();
            DataContext = new SearchingNagScreenViewModel();
            foreach (CoverSearcher cs in App.AvailableCoverSearchers)
            {
                cs.CoverFound += new CoverSearcher.CoverFoundHandler(CoverSearcher_CoverFound);
                ((InternetCoverStorage)App.CoverImageCache).ImageDownloaded += new InternetCoverStorage.ImageDownloadedDelegate(ProgressBarView_ImageDownloaded);
                cs.SearchCompleted += new EventHandler(CoverSearcher_SearchCompleted);
                cs.SearchStarted += new EventHandler(CoverSearcher_SearchStarted);
            }
        }

        public void ShowNagScreen()
        {
            this.Dispatcher.Invoke((Action)(() =>
             {
                 previewCover.Source = null;
                 this.Owner = App.MainView;
                 this.Show();
             }));
        }

        void CoverSearcher_SearchStarted(object sender, EventArgs e)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                if (fakePB != null)
                    fakePB.IsIndeterminate = true;

                ((SearchingNagScreenViewModel)DataContext).ClearCovers();
                previewCover.Source = null;
            }));
        }

        void CoverSearcher_CoverFound(string displayName, SearchMethod method, Uri urlFound)
        {
           this.Dispatcher.Invoke((Action)(() =>
           {
               //display the correct tab
               switch (method)
               {
                   case SearchMethod.Album: { summaryTC.SelectedIndex = 0; break; }
                   case SearchMethod.Song: { summaryTC.SelectedIndex = 1; break; }
                   case SearchMethod.Artist: { summaryTC.SelectedIndex = 2; break; }
               }
               ((SearchingNagScreenViewModel)DataContext).AddSongToDisplay(new CoverThumb(displayName, urlFound),method);
           }));
        }

        public void CloseWindow() {
            this.Dispatcher.Invoke((Action)(() =>
            {
                this.Visibility = System.Windows.Visibility.Hidden;
            }));
        }

        void CoverSearcher_SearchCompleted(object sender, EventArgs e)
        {
           // CloseWindow();
            this.Dispatcher.Invoke((Action)(() =>
            {
                fakePB.IsIndeterminate = false;
            }));
            
        }

        void ProgressBarView_ImageDownloaded(System.Drawing.Image image)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                if (image != null){
                    BitmapImage preview = ImageHelper.ImageToBitmapImage(image);
                    previewCover.Source = preview;
                }
            }));
        }

        private void albumLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            this.Dispatcher.Invoke((Action)(() =>
            {
                DisplayCover(((SearchingNagScreenViewModel)DataContext).AlbumCoversFound, albumLB.SelectedIndex);
            }));
        }

        private void DisplayCover(MultithreadingObservableCollection<CoverThumb> collection, int index)
        {
            if (index < 0)
                return;

            CoverThumb c = collection[index];
            if (c != null)
            {
                try
                {
                    if (App.CoverImageCache.ContainsCover(c.CachedUrl.AbsoluteUri))
                    {
                        System.Drawing.Image cachedCover = App.CoverImageCache[c.CachedUrl.AbsoluteUri];
                        previewCover.Source = ImageHelper.ImageToBitmapImage(cachedCover);
                    }
                }
                catch (Exception imageException)
                {
                    //do not display any exception, maybe te cover is still not downloaded
                }
            }
        }

        private void songLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                DisplayCover(((SearchingNagScreenViewModel)DataContext).SongCoversFound, songLB.SelectedIndex);
            }));
        }

        private void artistLB_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                DisplayCover(((SearchingNagScreenViewModel)DataContext).ArtistCoversFound, artistLB.SelectedIndex);
            }));
        }

        private void summaryTC_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.Source is TabControl)
            {
                TabControl tc = e.Source as TabControl;
                CoverCollections collection = CoverCollections.Album;
                switch (tc.SelectedIndex)
                {
                    case 0: { collection = CoverCollections.Album; break; }
                    case 1: { collection = CoverCollections.Song; break; }
                    case 2: { collection = CoverCollections.Artist; break; }
                }
                ((SearchingNagScreenViewModel)DataContext).ChangeCurrentCollection(collection);
            }            
        }
    }
}
